const Base = require('./base.js'),
	userModel = think.mongoose('user'),
	errorInfo = require('../config/error-info');

module.exports = class extends Base {
	/**
	 * @author: ares
	 * @date: 2019/4/3 9:32
	 * @description: 新建
	 * @returns {Promise.<*>}
	 */
	async createAction() {
		let data = this.post(),
			insert = await userModel.$create(data);
		if (think.isNull(insert)) {
			return this.fail(...errorInfo.msg1011,'账号已存在');
		} else {
			return this.success();
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 13:38
	 * @description: 删除用户
	 * @returns {Promise.<*>}
	 */
	async deleteAction() {
		let _id = this.get('id'),
			del = await userModel.$delete(_id);
		if (del > 0) {
			return this.success();
		} else {
			return this.fail(...errorInfo.msg1121,'账号不存在');
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 10:29
	 * @description: 更新用户信息
	 * @returns {Promise.<*>}
	 */
	async updateAction() {
		let data = this.post(),
			update = await userModel.$update(data);
		if (update > 0) {
			return this.success();
		} else {
			return this.fail(...errorInfo.msg1131, '账号不存在');
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:45
	 * @description: 切换用户启用禁用状态
	 * @returns {Promise.<*>}
	 */
	async toggleStateAction() {
		let data = this.post(),
			toggle = await userModel.toggleState(data);
		if (toggle > 0) {
			return this.success();
		} else {
			return this.fail(...errorInfo.msg1131,'账号不存在');
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/3 11:45
	 * @description: 重置用户密码
	 * @returns {Promise.<*>}
	 */
	async resetPwdAction() {
		let data = this.post(),
			reset = await userModel.resetPwd(data);
		if (reset > 0) {
			return this.success();
		} else {
			return this.fail(...errorInfo.msg1131,'账号不存在');
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/10 16:48
	 * @description: 用户修改自己密码
	 */
	async modifyPwdAction() {
		let _id = this.post('id'),
			token = this.header('token'),
			oldPwd = this.post('oldPwd'),
			newPwd = this.post('newPwd'),
			// currentAccount = this.ctx.state.currentUser.account,
			result = await userModel.modifyPwd(_id, oldPwd, newPwd);
		if (think.isNumber(result) && result > 0) {
			return this.success();
		} else if(result.error) {
			return this.fail(...errorInfo.msg1131,result.error);
		} else {
			return this.fail(...errorInfo.msg1131);
		}
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 13:45
	 * @description: 分页查询用户,可搜索
	 * @returns {Promise.<*>}
	 */
	async getPageListAction() {
		let searchKey = this.get('searchKey'),
			currentPage = this.get('currentPage'),
			pageSize = this.get('pageSize'),
			result = await userModel.getPageList(searchKey, currentPage, pageSize);
		return this.success(result);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:41
	 * @description: 根据用户id获取用户信息
	 * @returns {Promise.<*>}
	 */
	async getByIdAction() {
		let _id = this.get('id'),
			result = await userModel.getById(_id);
		return this.success(result);
	}

	/**
	 * @author: ares
	 * @date: 2020/3/9 10:49
	 * @description: 根据账号获取账号信息
	 * @returns {*}
	 */
	async getByAccountAction(){
		let account = this.get('account'),
			result = await userModel.getByAccount(account);
		return this.success(result);
	}

	/**
	 * @author: ares
	 * @date: 2019/4/9 14:41
	 * @description: 根据token获取用户信息,实际是根据用户账号查询
	 * @returns {Promise.<*>}
	 */
	async getByTokenAction() {
		let token = this.header('token'),
			currentAccount = this.ctx.state.currentUser.account,
			result = await userModel.getByAccount(currentAccount);
		return this.success(result);
	}
};
